﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Collections;

namespace Nauka.engine
{
    public class StartingHands
    {
        private MainProgram mp;
        DataTable dt;
        DataTable result;
        private class Pairs
        {
            public Rank rank{ get; set;}
            public int count { get; set; }
            public float won { get; set; }
            public int times_won { get; set; }
        }
        private class Suited
        {
            public Rank rank_high{ get; set; }
            public Rank rank_low{ get; set; }
            public int count { get; set; }
            public float won { get; set; }
            public int times_won { get; set; }
        }
        private class Unsuited
        {
            public Rank rank_high { get; set; }
            public Rank rank_low { get; set; }
            public int count { get; set; }
            public float won { get; set; }
            public int times_won { get; set; }
        }
        ArrayList pairs = new ArrayList();
        ArrayList suited = new ArrayList();
        ArrayList unsuited = new ArrayList();

        public StartingHands(MainProgram mp)
        {
            this.mp = mp;
            dt = mp.dbconn.getStartingHands(mp.statsFilter.hero);
            result = new DataTable();
            result.Columns.Add("Hand");
            result.Columns.Add("Times");
            result.Columns.Add("%Won");
            result.Columns.Add("$ Won");
            result.Columns.Add("$/Hand");
        }

        private void insert_pair(Rank r, float amount){
            bool exist = false;
            foreach (Pairs p in pairs)
            {
                if (p.rank == r)
                {
                    exist = true;
                    p.count++;
                    p.won += amount;
                    if (amount > 0)
                        p.times_won++;

                }
            }
            if (exist == false)
            {
                Pairs pair = new Pairs();
                pair.rank = r;
                pair.count = 1;
                pair.won = amount;
                if (amount > 0)
                    pair.times_won = 1;
                else
                    pair.times_won = 0;
                pairs.Add(pair);
            }
        }

        private void insert_suited(Rank r1, Rank r2, float amount)
        {
            bool exist = false;
            Rank rank_high, rank_low;
            if (r1 > r2)
            {
                rank_high = r1;
                rank_low = r2;
            }
            else
            {
                rank_high = r2;
                rank_low = r1;
            }
            foreach (Suited s in suited)
            {
                if (s.rank_high == rank_high && s.rank_low == rank_low)
                {
                    exist = true;
                    s.count++;
                    s.won += amount;
                    if (amount > 0)
                        s.times_won++;
                }
            }
            if (exist == false)
            {
                Suited s = new Suited();
                s.rank_high = rank_high;
                s.rank_low = rank_low;
                s.count = 1;
                s.won = amount;
                if (amount > 0)
                    s.times_won = 1;
                else
                    s.times_won = 0;
                suited.Add(s);
            }
        }

        private void insert_unsuited(Rank r1, Rank r2, float amount)
        {
            bool exist = false;
            Rank rank_high, rank_low;
            if (r1 > r2)
            {
                rank_high = r1;
                rank_low = r2;
            }
            else
            {
                rank_high = r2;
                rank_low = r1;
            }
            foreach (Unsuited s in unsuited)
            {
                if (s.rank_high == rank_high && s.rank_low == rank_low)
                {
                    exist = true;
                    s.count++;
                    s.won += amount;
                    if (amount > 0)
                        s.times_won++;
                }
            }
            if (exist == false)
            {
                Unsuited s = new Unsuited();
                s.rank_high = rank_high;
                s.rank_low = rank_low;
                s.count = 1;
                s.won = amount;
                if (amount > 0)
                    s.times_won = 1;
                else
                    s.times_won = 0;
                unsuited.Add(s);
            }
        }

        public void count()
        {
            foreach (DataRow dr in dt.Rows)
            {
                Card card1 = new Card(Convert.ToInt16(dr[0]));
                Card card2 = new Card(Convert.ToInt16(dr[1]));
                if (card1.rank == card2.rank)
                    insert_pair(card1.rank, Convert.ToInt16(dr[2]));
                else if (card1.color == card2.color)
                    insert_suited(card1.rank, card2.rank, Convert.ToInt16(dr[2]));
                else
                    insert_unsuited(card1.rank, card2.rank, Convert.ToInt16(dr[2]));
            }
        }

        public DataTable generateDataTable()
        {
            result.Clear();
            foreach (Pairs p in pairs)
            {
                string s = Rank_string.getString(p.rank);
                object[] row = {s+ "" +s, p.count, p.times_won*100.0/p.count, p.won, (p.won/p.count*1.0) };
                result.Rows.Add(row);
            }
            foreach (Suited s in suited)
            {
                string s1 = Rank_string.getString(s.rank_high);
                string s2 = Rank_string.getString(s.rank_low);
                object[] row = { s1 + "" + s2 + "s", s.count, s.times_won * 100.0 / s.count, s.won, (s.won / s.count * 1.0) };
                result.Rows.Add(row);
            }
            foreach (Unsuited u in unsuited)
            {
                string s1 = Rank_string.getString(u.rank_high);
                string s2 = Rank_string.getString(u.rank_low);
                object[] row = { s1 + "" + s2 + "o", u.count, u.times_won * 100.0 / u.count, u.won, (u.won / u.count * 1.0) };
                result.Rows.Add(row);
            }
            return result;
        }

    }
}
